package com.palm.hubo2o.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StartAppUtil {
    public static final String packageName = "com.yxing.yinfuka";
    public static final String login = "login";
    public static final String pay = "pay";
    public static final String bindPointCard = "pointCardBind";

    /**
     */
    private static Intent createIntent(Context context, String info, Map<String, String> params) {

        // 通过包名获取此APP详细信息，包括Activities、services、versioncode、name等等
        PackageInfo packageinfo = null;
        try {
            packageinfo = context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(context, "未安装银富卡app，无法执行跳转操作", Toast.LENGTH_SHORT).show();
        }
        if (packageinfo == null) {
            return null;
        }

        // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(packageinfo.packageName);

        // 通过getPackageManager()的queryIntentActivities方法遍历
        List<ResolveInfo> resolveinfoList = context.getPackageManager().queryIntentActivities(resolveIntent, 0);

        ResolveInfo resolveinfo = resolveinfoList.iterator().next();
        if (resolveinfo != null) {
            // packagename = 参数packname
            String packageName = resolveinfo.activityInfo.packageName;
            // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式：packagename.mainActivityname]
            String className = resolveinfo.activityInfo.name;
            // LAUNCHER Intent
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            // 设置ComponentName参数1:packagename参数2:MainActivity路径
            ComponentName cn = new ComponentName(packageName, className);

            intent.setComponent(cn);
            intent.putExtra("appName", "yucaitong");
            intent.putExtra("info", info);
            if (pay.equals(info)||bindPointCard.equals(info)) {
                for (Map.Entry<String, String> kv : params.entrySet()) {
                    intent.putExtra(kv.getKey(), kv.getValue());
                }
            }
            return intent;
//            ((MainActivity) context).startActivityForResult(intent, 3);
        }
        return null;
    }

    public static Intent prepareToLogin(Context context) {
        return createIntent(context, login, null);
    }

    public static Intent prepareToPay(Context context, Map<String, String> params) {
        return createIntent(context, pay, params);
    }
    public static Intent prepareToBindPointCard(Context context, Map<String, String> params) {
        return createIntent(context, bindPointCard, params);
    }


    public static Intent jumpToBank(Context context) {
        return createIntent(context, null, null);
    }

    /**
     * 处理返回结果
     *
     * @param arg0
     * @param arg1
     * @param arg2
     * @return
     */
    public static boolean handleResult(int arg0, int arg1, Intent arg2) {
        // 处理登录页面返回参数
        if (arg0 == 3) {
            if (arg1 == 4) {
                String result = arg2.getStringExtra("result");
                if (result.equalsIgnoreCase("success")) {
                    return true;
                } else if (result.equalsIgnoreCase("fail")) {
                } else if (result.equalsIgnoreCase("cancel")) {
                }

            }
        }
        return false;
    }

    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param context
     * @param packageName ：应用包名
     * @return
     */
    public boolean isAvilible(Context context, String packageName) {
        // 获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        // 用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        // 从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        // 判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }
}
